//
//  SCB_Today_Music_ViewController.m
//  Time
//
//  Created by dllo on 16/7/26.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "SCB_Today_Music_ViewController.h"
#import "SCB_Today_Detail_Model.h"
#import "Today_Today_detail_MusicTableViewCell.h"

@interface SCB_Today_Music_ViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,retain) SCB_Today_Detail_Model *model;

@property (nonatomic,retain) UILabel *commentLabel;

@property (nonatomic,retain) UILabel *likesLabel;

@property (nonatomic,retain) UITableView *tableView;

@property (nonatomic,retain) UIImageView *backgroundImageView;
@end

@implementation SCB_Today_Music_ViewController

-(void)dealloc{
    [_contentId release];
    [_commentLabel release];
    [_likesLabel release];
    [_tableView release];
    [_model release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.backgroundImageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:self.backgroundImageView];
    
    UIBlurEffect *blu = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *view = [[UIVisualEffectView alloc] initWithEffect:blu];
    [self.backgroundImageView addSubview:view];
    view.frame = [UIScreen mainScreen].bounds;
    view.alpha = 1.0;

    NSLog(@"%@",self.contentId);
    
    
    [self handleDate];
//    [self handleMusicDate];
    [self createToolView];
    [self createTableView];
    
}

-(void)handleDate{
    NSString *urlStr = @"http://api.pianke.me/version4.0/music/info.php?sig=EC8179AD63CAF9ECB478C0143F5E472E";
    NSString *body = [NSString stringWithFormat:@"contentid=%@&sized=1",self.contentId];
    NSDictionary *header = @{@"Authorization":HEADER};
    [SCBNetTool POST:urlStr withBody:body withBodyStyle:SCBBodyString withHeaderFile:header withResponse:SCBJSON withSuccess:^(id result) {
        NSLog(@"读取成功");
        NSDictionary *dictionary = result;
        NSDictionary *dic = [dictionary valueForKey:@"data"];
        self.model = [[SCB_Today_Detail_Model alloc] init];
        [self.model setValuesForKeysWithDictionary:dic];
        
        self.likesLabel.text = [NSString stringWithFormat:@"%@",self.model.likes];
        self.commentLabel.text = [NSString stringWithFormat:@"%@",self.model.comments];
        [self.backgroundImageView setImageWithURL:[NSURL URLWithString:self.model.scbSongInfo.albumcover] placeholderImage:nil];
//        NSLog(@"%@",self.model.scbSongInfo.albumcover);
        [self.tableView reloadData];
    } withFaolure:^(NSError *error) {
        NSLog(@"音乐文本内容读取失败%@",error);
    }];
    
}

-(void)createToolView{
    UIView *toolView = [[UIView alloc] init];
    toolView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:toolView];
    [toolView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
        make.bottom.equalTo(self.view.mas_bottom).offset(0);
        make.height.mas_equalTo(SWIDTH * 0.2);
    }];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [toolView addSubview:backButton];
    [backButton setImage:[UIImage imageNamed:@"返回(white).png"] forState:UIControlStateNormal];
    [backButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(toolView.mas_left).offset(9);
        make.top.equalTo(toolView.mas_top).offset(9);
        make.bottom.equalTo(toolView.mas_bottom).offset(9);
        make.width.mas_equalTo(SWIDTH * 0.2 - 18);
    }];
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [toolView addSubview:shareButton];
    [shareButton setImage:[UIImage imageNamed:@"分享(white).png"] forState:UIControlStateNormal];
    [shareButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(toolView.mas_right).offset(9);
        make.top.equalTo(toolView.mas_top).offset(9);
        make.bottom.equalTo(toolView.mas_bottom).offset(9);
        make.width.mas_equalTo(SWIDTH * 0.2 - 18);
    }];
    [shareButton addTarget:self action:@selector(shareButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.commentLabel = [[UILabel alloc] init];
    self.commentLabel.textColor = [UIColor whiteColor];
    [toolView addSubview:self.commentLabel];
    [self.commentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(shareButton.mas_left).offset(9);
        make.top.equalTo(toolView.mas_top).offset(9);
        make.bottom.equalTo(toolView.mas_bottom).offset(9);
        make.width.mas_equalTo(SWIDTH * 0.2 - 18);
    }];
    
    
    UIButton *commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [toolView addSubview:commentButton];
    [commentButton setImage:[UIImage imageNamed:@"消息.png"] forState:UIControlStateNormal];
    [commentButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.commentLabel.mas_left).offset(9);
        make.top.equalTo(toolView.mas_top).offset(9);
        make.bottom.equalTo(toolView.mas_bottom).offset(9);
        make.width.mas_equalTo(SWIDTH * 0.2 - 18);
    }];
    [commentButton addTarget:self action:@selector(commentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.likesLabel = [[UILabel alloc] init];
    self.likesLabel.textColor = [UIColor whiteColor];
    [toolView addSubview:self.likesLabel];
    [self.likesLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(commentButton.mas_left).offset(9);
        make.top.equalTo(toolView.mas_top).offset(9);
        make.bottom.equalTo(toolView.mas_bottom).offset(9);
        make.width.mas_equalTo(SWIDTH * 0.2 - 18);
    }];
    
    UIButton *collButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [toolView addSubview:collButton];
    [collButton setImage:[UIImage imageNamed:@"爱心 (1).png"] forState:UIControlStateNormal];
    [collButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.likesLabel.mas_left).offset(0);
        make.top.equalTo(toolView.mas_top).offset(9);
        make.bottom.equalTo(toolView.mas_bottom).offset(9);
        make.width.mas_equalTo(SWIDTH * 0.2 - 18);
    }];
    [collButton addTarget:self action:@selector(collButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)backButtonAction:(UIButton *)button{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)collButtonAction:(UIButton *)button{
    NSLog(@"收藏");
}

-(void)commentButtonAction:(UIButton *)button{
    NSLog(@"评论");
}

-(void)shareButtonAction:(UIButton *)button{
    NSLog(@"分享");
}

-(void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, SHEIGHT - SWIDTH * 0.2) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[Today_Today_detail_MusicTableViewCell class] forCellReuseIdentifier:@"Today_Music_TableView_Cell"];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    Today_Today_detail_MusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Today_Music_TableView_Cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    /**
     *  传值
     */
    cell.model = self.model;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    /**
     *  - -||
     */
    return 150 + SWIDTH * 0.6 + [Tools getTextHeight:self.model.title withWidth:SWIDTH - 40 withFontSize:24.0] + [Tools getTextHeight:self.model.text withWidth:SWIDTH - 40 withFontSize:12.0];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)prefersStatusBarHidden{
    return YES;
}

@end
